package mc2.ui;
import mc2.*;
import java.util.*;
import javax.swing.JOptionPane;

import de.waldemar.ChatIcons;
import de.waldemar.WindowUtils;

public class FrmUserShare extends javax.swing.JFrame {
    
    private User _user;
    //<User,frmUserShare>
    private static TreeMap _userMap=new TreeMap();
    //<String,AttachmentMessage>
    private TreeMap _filecache=new TreeMap();
    
    
    public static FrmUserShare ForUser(User iUser){
        if(!_userMap.containsKey(iUser))
            _userMap.put(iUser,new FrmUserShare(iUser));
        FrmUserShare retFrm=(FrmUserShare)_userMap.get(iUser);
        retFrm.UpdateShare();
        return retFrm;
    }
    
    public void AddToCache(AttachmentMessage iMsg){
        _filecache.put(iMsg.GetFileName(),iMsg);
        if(ltFileList.getSelectedValue()!=null  && ltFileList.getSelectedValue().equals(iMsg.GetFileName())){
            tbContent.setText(iMsg.GetContent());
        }
    }
    
    private FrmUserShare(User iUser) {
        _user=iUser;
        initComponents();
        setTitle(iUser.GetName() + " Share list");
        setSize(400,300);
        setIconImage(ChatIcons.ICON_DEFAULT);
        WindowUtils.optimizedCentrilized(this);   
    }

    private boolean UpdateShare(){
        if(Manager.GetInstance().GetUserShare(_user)==null){
            JOptionPane.showMessageDialog(this,"User has no active share","Error",JOptionPane.ERROR_MESSAGE);
            setVisible(false);
            return false;
        }
        ltFileList.setModel(new StringListModel(Manager.GetInstance().GetUserShare(_user)));
        return true;
    }
    public void Show(){
        setVisible(true);
        requestFocus();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        mnuPopup = new javax.swing.JPopupMenu();
        mnuSaveas = new javax.swing.JMenuItem();
        panSplit = new javax.swing.JSplitPane();
        scrollFileList = new javax.swing.JScrollPane();
        ltFileList = new javax.swing.JList();
        scrollContent = new javax.swing.JScrollPane();
        tbContent = new javax.swing.JTextArea();

        mnuSaveas.setLabel("Save as...");
        mnuSaveas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuSaveasActionPerformed(evt);
            }
        });

        mnuPopup.add(mnuSaveas);

        getContentPane().setLayout(new java.awt.GridLayout(1, 0));

        setTitle("Share list");
        setName("frmUserShare");
        panSplit.setDividerLocation(140);
        ltFileList.setFont(new java.awt.Font("Tahoma", 0, 10));
        ltFileList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ltFileListMouseClicked(evt);
            }
        });

        scrollFileList.setViewportView(ltFileList);

        panSplit.setLeftComponent(scrollFileList);

        tbContent.setBackground(new java.awt.Color(204, 204, 204));
        tbContent.setColumns(20);
        tbContent.setRows(5);
        scrollContent.setViewportView(tbContent);

        panSplit.setRightComponent(scrollContent);

        getContentPane().add(panSplit);

    }// </editor-fold>//GEN-END:initComponents

    private void mnuSaveasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuSaveasActionPerformed
        if(ltFileList.getSelectedIndex()>=0){
            if(!_filecache.containsKey(ltFileList.getSelectedValue()))
                return;
            
            java.io.File oFile=FileDialog.SaveFileDialog();
            
            if(oFile==null)
                return;
            try {
                AttachmentMessage curSel=(AttachmentMessage)_filecache.get(ltFileList.getSelectedValue());
                curSel.SaveToFile(oFile.getPath());
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this,ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
                ex.printStackTrace();
            }
            
        }
    }//GEN-LAST:event_mnuSaveasActionPerformed

    private void ltFileListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ltFileListMouseClicked
        if(evt.getButton()!=evt.BUTTON1)
            mnuPopup.show(ltFileList,evt.getX(),evt.getY());
        
        if(ltFileList.getSelectedValue()==null)
               return;
        if(!_filecache.containsKey(ltFileList.getSelectedValue())){
            ServiceMessage newMsg=new ServiceMessage(Manager.GetInstance().GetMe(),_user,ServiceMessage.CODE_ASK_FILE,(String)ltFileList.getSelectedValue());
            Manager.GetInstance().GetDispatcher().DispatchToAll(newMsg);
        }else{
            tbContent.setText(((AttachmentMessage)_filecache.get(ltFileList.getSelectedValue())).GetContent());
        }
    }//GEN-LAST:event_ltFileListMouseClicked
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList ltFileList;
    private javax.swing.JPopupMenu mnuPopup;
    private javax.swing.JMenuItem mnuSaveas;
    private javax.swing.JSplitPane panSplit;
    private javax.swing.JScrollPane scrollContent;
    private javax.swing.JScrollPane scrollFileList;
    private javax.swing.JTextArea tbContent;
    // End of variables declaration//GEN-END:variables
    
}

class StringListModel extends javax.swing.AbstractListModel{
    String[] _list;
    public StringListModel(String[] iList){
        _list=iList;
    }
    public int getSize() { return _list.length; }
    public Object getElementAt(int i) { return _list[i]; }
}
